﻿using Client.Common.Extensions;
using Client.Todo.Common.Models;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using MaterialDesignColors.ColorManipulation;
using System.Windows.Media;

namespace Client.Todo.ViewModels
{
    /// <summary>
    /// 个性化VM
    /// </summary>
    public class SkinViewModel : ViewModelBase
    {

        private readonly PaletteHelper _paletteHelper = new PaletteHelper();

        public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;
        public ICommand ChangeHueCommand { get; }

        private bool _isDarkTheme;
        public bool IsDarkTheme
        {
            get => _isDarkTheme;
            set
            {
                if (SetProperty(ref _isDarkTheme, value))
                {
                    ModifyTheme(theme => theme.SetBaseTheme(value ? Theme.Dark : Theme.Light));
                }
            }
        }

        private static void ModifyTheme(Action<ITheme> modificationAction)
        {
            var paletteHelper = new PaletteHelper();
            ITheme theme = paletteHelper.GetTheme();

            modificationAction?.Invoke(theme);

            paletteHelper.SetTheme(theme);
        }
        #region 切换色调

        /// <summary>
        /// 切换色调 - 命令
        /// </summary>
        private DelegateCommand<object> _changeHueCmd;

        /// <summary>
        /// 切换色调 - 命令
        /// </summary>
        public DelegateCommand<object> ChangeHueCmd =>
            _changeHueCmd ?? (_changeHueCmd = new DelegateCommand<object>(ExecuteChangeHue));

        /// <summary>
        /// 执行 - 切换色调
        /// </summary>    
        void ExecuteChangeHue(object obj)
        {
            var hue = (Color)obj;

            ITheme theme = _paletteHelper.GetTheme();

            theme.PrimaryLight = new ColorPair(hue.Lighten());
            theme.PrimaryMid = new ColorPair(hue);
            theme.PrimaryDark = new ColorPair(hue.Darken());

            _paletteHelper.SetTheme(theme);
        }

        #endregion 切换色调

        public SkinViewModel() 
        {

        }

    }
}
